/**
 * 流程信息表 列表页
 *
 * 作者 fenghaijun
 * 日期 2024-07-15 17:34:52
 */
layui.use(['jquery', 'laydate', 'febs', 'form', 'table', 'commonJS', 'validate',], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        commonJS = layui.commonJS,
        validate = layui.validate,
        $view = $('#febs-workflowInfo'),
        $query = $view.find('#query'),
        $searchForm = $view.find('#workflowInfo-table-form'),
        tableIns;

    form.verify(validate);
    form.render();

    initTable();

    function initTable() {
        tableIns = febs.table.init({
            elem: $view.find('table'),
            id: 'workflowInfoTable',
            height: getTableHeight(),
            skin: '',
            cols: [[
                {type: 'numbers',title:'序号',width: 60,align: 'center'},
                {field: 'workflowId', title: '主键id', width: 120, hide: true},
                {field: 'tenantName', title: '所属公司', width: 280, hide:!hasHeadCompanyRole,
                    templet: function (d) {
                        let tenantName = "--"
                        let tenantArray = tenantList.filter(item => item.tenantId == d.tenantId);
                        if(null != tenantArray && tenantArray.length > 0){
                            tenantName = tenantArray[0].tenantName;
                        }
                        return tenantName;
                    } },
                {field: 'workflowName', title: '流程名称', minWidth: 200,
                    templet:function (d) {
                        return `<a lay-event="detail" style="color:blue;cursor: pointer">${d.workflowName}</a>`
                    }
                },
                {field: 'remark', title: '流程说明', minWidth: 200},
                {title: '状态', toolbar: '#status-bar', width: 120,align: 'center'},
                {title: '操作', toolbar: '#workflowInfo-bar', align: 'center', width: 130}
            ]]
        });
        tableIns.reload({url: ctx + 'workflowInfo/list', where: getQueryParams(), page: {curr: 1}});
    }

    table.on('sort(workflowInfoTable)', function (obj) {
        tableIns.reload({
            initSort: obj,
            where: $.extend(getQueryParams(), {
                field: obj.field,
                order: obj.type
            }),
            autoSort: true
        });
    });

    table.on('tool(workflowInfoTable)', function (obj) {
        if (obj.event === 'detail') {
            window.detail(obj);
        }
    });

    function reloadPage(curr) {
        var _curr = $view.find(".layui-laypage-em").next().html();
        if (!!curr) {
            _curr = curr;
        }
        tableIns.reload({where: getQueryParams(), page: {curr: _curr}});
    }

    $query.on('click', function () {
        reloadPage(1);
    });

    table.on('tool(workflowInfoTable)', function (obj) {
        if (obj.event === 'detail') {
            window.detail(obj);
        } else if (obj.event === 'edit') {
            edit(obj);
        }
    });

    form.on('switch(workflowStatus)', function (obj) {
        let workflowStatus = "0";
        let workflowStatusText = "禁用";
        var checked = obj.elem.checked;
        if (checked) {
            workflowStatus = "1";
            workflowStatusText = "启用"
        }
        let model = {
            workflowId: this.value,
            workflowStatus: workflowStatus
        }
        febs.modal.confirm('提示', '是否'+workflowStatusText+'流程？',function (){
            febs.postArray(ctx + 'workflowInfo/updateStatus', model, function () {
                layer.closeAll();
                febs.alert.success(workflowStatusText+'成功');
                reloadPage();
            });
        },function (){
            obj.elem.checked = !checked;
            form.render()
            layer.closeAll();
        });


    });

    function edit(obj) {
        var model = obj.data
        if (model.workflowStatus != "0") {
            febs.alert.warn('流程状态不支持修改');
            return false;
        }
        febs.modal.open('修改', 'workflowInfo/update/' + model.workflowId, {
            btn: ['提交','清空审批流'],
            area: ['98%', '98%'],
            yes: function (index, layero) {
                $('#workflowInfo-edit').find('#submit').trigger('click');
            },
            btn2: function (index, layero) {
                clearWorkFlow()
                return false;
            }
        });
    }

    window.detail = function (obj) {
        var model = obj.data
        febs.modal.open('详细', 'workflowInfo/detail/' + model.workflowId, {
            btn: ['关闭'],
            area: ['98%', '98%'],
            data:{
                workflowNodeId:null
            },
            yes: function (index, layero) {
                layer.closeAll();
            },
        });
    }

    function getQueryParams() {
        return {
            tenantId:$searchForm.find("select[name='tenantId']").find("option:selected").val(),
            workflowName: $searchForm.find('input[name="workflowName"]').val()?.trim(),
            invalidate_ie_cache: new Date()
        };
    }

    function getTableHeight() {
        return commonJS.calcTableHeight();
    }

    $(window).resize(function () {
        tableIns.reload({height: getTableHeight()});
    });

});